package com.own.component.util.position.model;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * Ip2RegionModel
 *
 * @author chenxueli
 * @date 2022-09-21 17:15:00
 */
@Getter
@Setter
public class Ip2RegionModel implements Serializable {

    /**
     * ip
     */
    private String ip;

    /**
     * 国家
     */
    private String country;

    /**
     * 归属地
     */
    private String region;

    /**
     * 省份
     */
    private String province;

    /**
     * 城市
     */
    private String city;

    /**
     * 内容
     */
    private String content;

    public Ip2RegionModel(String ip, String content) {
        this.ip = ip;
        this.content = content;
        var array = content.trim().split("\\|");
        if (array.length > 0) {
            this.country = array[0];
            if (array.length > 3) {
                this.province = array[2];
                if (array.length > 4) {
                    this.city = array[3];
                }
            }
            // 计算归属地
            calcRegion();
        }
    }

    /**
     * 计算归属地
     */
    private void calcRegion() {
        this.region = "中国".equals(country) ? province + city : country;
    }

    @Override
    public String toString() {
        return region;
    }

}
